
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==

using System;
using System.Xml;

namespace Swaf
{
	/// <summary>
	/// Summary description for BaseProperty.
	/// </summary>
	public class BaseProperty : IProperty
	{
		protected string m_name="";
		protected object m_val="";
		protected bool m_isSerialized=false;
		protected string m_type="";
		protected bool m_ignoreExceptions=false;
		public BaseProperty()
		{
		}
		public BaseProperty(string name, object val, bool isSerialized, bool ignoreExceptions)
		{
			m_name=name;
			m_val = val;
			m_isSerialized=isSerialized;
			if (! m_isSerialized)
				m_type = val.GetType().Name;
			m_ignoreExceptions = ignoreExceptions;
		}
		public BaseProperty(XmlElement node, bool ignoreExceptions)
		{
			m_name = node.GetAttribute("name");
			m_val = node.InnerXml;
			m_isSerialized = true;
			m_ignoreExceptions = ignoreExceptions;
		}
		#region IProperty Members
		public bool ignoreExceptions
		{
			get
			{
				return m_ignoreExceptions;
			}
			set
			{
				m_ignoreExceptions = value;
			}	
		}
		public bool isSerialized
		{
			get
			{
				return m_isSerialized;
			}
			set
			{
				m_isSerialized=value;
			}
		}
		public object val
		{
			get
			{
				return m_val;
			}
			set
			{
				m_val = value;
			}
		}

		public string name
		{
			get
			{
				return m_name;
			}
			set
			{
				m_name=value;
			}
		}
		public void callSet(object onThis)
		{
			try
			{
				System.Type clas = onThis.GetType();
				System.Reflection.PropertyInfo member = clas.GetProperty(m_name);
				if (member.CanWrite)
				{
					//If m_val is still serialized, deserialize on first use.
					if (m_isSerialized)
					{
						m_type = member.PropertyType.Name;
						if (!(m_type.Equals("IBizObj") || m_type.Equals("IBizObjList")))
							m_type=member.PropertyType.FullName;
						m_val = Serialization.deserialize(m_type,(string) m_val);
						m_isSerialized = false;
					}
					Cmn.callPropertySet(onThis,m_name, m_val);
				}
				else if (! m_ignoreExceptions)
				{
					throw new InvalidPropertyException(string.Format("Unable to set property {0} to value {1}", m_name,m_val==null?"null":m_val.ToString()));
				}
			}
			catch(Exception e)
			{
				throw new InvalidPropertyException(string.Format("Unable to set property {0} to value {1}", m_name,m_val==null?"null":m_val.ToString()),e);
			}
		}

		#endregion
	}
}
